package com.inossem.warehousestorage_inossem.activity.query;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.query.extra.QueryExtra;
import com.inossem.warehousestorage_inossem.adapter.query.QueryMaterielDetailAdapter;
import com.inossem.warehousestorage_inossem.bean.query.QueryMaterielDetailBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.query.QueryBatchMaterielDetailRequest;
import com.inossem.warehousestorage_inossem.request.query.QuerySingleMaterielDetailRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;


//验收入库
public class QueryMaterielDetailActivity extends BaseActivity {

    public static final String BATCH = "batch";//批量

    public static final String SINGLE = "single";//单体

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView materielList;

    private QueryMaterielDetailAdapter adapter;

    private String mType, mId, mCondition;

    private QueryMaterielDetailBean.DataBean singleBean, batchBean;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_query_materiel_detail);
        initView(QueryMaterielDetailActivity.this);
        initData(QueryMaterielDetailActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        materielList = (PullableListView) activity.findViewById(R.id.queryMaterielList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.queryMaterielListRefresh);
    }

    private void initData(Activity activity) {
        title.setText("物料库存");
        ActivityUtils.initBackAndFunction(activity, "查询", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        materielList.setPullDownFlag(false);
        materielList.setPullUpFlag(false);
        getExtra(activity);
        mCondition = "";
        getData(activity, mType);
        if (SINGLE.equals(mType)) {
            setmScanListener((String scanResult, int length) -> {
                String type = QRCodeUtils.getQRCodeType(scanResult);
                switch (type) {
                    case QRCodeConstant.TYPE_BATCH:
                        mCondition = QRCodeUtils.DecryptQRCode(scanResult, QRCodeConstant.PARAMETER_MATERIEL_CODE);
                        break;
                    case QRCodeConstant.TYPE_MATERIEL_CODE:
                        mCondition = QRCodeUtils.DecryptQRCode(scanResult);
                        break;
                }
                getDataSingle(activity);
            }, QRCodeConstant.TYPE_BATCH, QRCodeConstant.TYPE_MATERIEL_CODE);
        }
    }

    private void getExtra(Activity activity) {
        mType = activity.getIntent().getStringExtra(QueryExtra.TYPE);
        mId = activity.getIntent().getStringExtra(QueryExtra.ID);
    }

    private void addQueryListData(Context context, QueryMaterielDetailBean.DataBean bean, QueryMaterielDetailBean.DataBean.BatchListBean batchItem, ListView listView, String type, boolean flag) {
        if (flag) {
            adapter = new QueryMaterielDetailAdapter(context, bean, batchItem, mCondition, type, new QueryMaterielDetailListener() {
                @Override
                public void onNoDoubleClickListener(String condition) {
                    mCondition = condition;
                    getDataSingle(context);
                }

                @Override
                public void onChangeBatch(QueryMaterielDetailBean.DataBean.BatchListBean batchItem) {
                    addQueryListData(context, singleBean, batchItem, materielList, SINGLE, true);
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Context context, String type) {
        switch (type) {
            case BATCH:
                getDataBatch(context);
                break;
            case SINGLE:
                initDataSingle(context);
                break;
        }
    }

    private void initDataSingle(Context context) {
        addQueryListData(context, null, new QueryMaterielDetailBean.DataBean.BatchListBean(), materielList, SINGLE, true);
    }

    private void getDataSingle(Context context) {
        QuerySingleMaterielDetailRequest.request(context, mCondition, new GsonBeanListener<QueryMaterielDetailBean>() {
            @Override
            public void onSuccess(QueryMaterielDetailBean bean) {
                singleBean = bean.getData();
                if (singleBean.getBatchList() != null && !singleBean.getBatchList().isEmpty()) {
                    addQueryListData(context, singleBean, singleBean.getBatchList().get(0), materielList, SINGLE, true);
                } else {
                    Utils.middleToast(context, "未查到数据");
                    initDataSingle(context);
                }
            }
        });
    }

    private void getDataBatch(Context context) {
        QueryBatchMaterielDetailRequest.request(context, mId, new GsonBeanListener<QueryMaterielDetailBean>() {
            @Override
            public void onSuccess(QueryMaterielDetailBean bean) {
                batchBean = bean.getData();
                addQueryListData(context, batchBean, batchBean.getBatchList().get(0), materielList, BATCH, true);
                bean.getData();
            }
        });
    }

    public interface QueryMaterielDetailListener {
        void onNoDoubleClickListener(String materielCode);

        void onChangeBatch(QueryMaterielDetailBean.DataBean.BatchListBean batchItem);
    }
}
